struct VIn
{
	float3 pos : POSITION;
	float2 uv : TEXCOORD0;
	float4 color : COLOR;
};

struct VOut
{
	float4 pos : POSITION;
	float2 uv : TEXCOORD0;
	float4 color : COLOR;
};

float4 border_color = {0.0f, 0.0f, 0.0f, 1.0f};
float border_thickness = 32.0f;

const float dst = sqrt(3.0f)/4.0f;
const float3 line1 = {cos(radians(30)), sin(radians(30)), sqrt(3.0f)/4.0f};
const float3 line2 = {cos(radians(330)), sin(radians(330)), sqrt(3.0f)/4.0f};
const float3 line3 = {cos(radians(150)), sin(radians(150)), sqrt(3.0f)/4.0f};
const float3 line4 = {cos(radians(210)), sin(radians(210)), sqrt(3.0f)/4.0f};

texture tex : SPRITE_TEXTURE;

VOut VS_HexBatch(VIn IN)
{
	VOut OUT;
	OUT.pos = float4(IN.pos, 1.0f);
	OUT.uv = IN.uv;
	OUT.color = IN.color;

	return OUT;
}

float4 PS_HexBatch(VOut IN) : COLOR
{
	float2 p = IN.uv - float2(0.5f, 0.5f);

	float d1 = dot(p, line1.xy) + line1.z;
	float d2 = dot(p, line2.xy) + line2.z;
	float d3 = dot(p, line3.xy) + line3.z;
	float d4 = dot(p, line4.xy) + line4.z;
	float d5 = min(dst + p.y, dst - p.y);

	float mind = min(min(min(d1, d2), min(d3, d4)), d5);

	if(mind < 0.0f)
		return float4(0.0f, 0.0f, 0.0f, 0.0f);

	float t = saturate((1.0f/(mind)) / border_thickness);	
	return float4(lerp(IN.color.rgb, border_color.rgb, t), IN.color.a); 	
}

technique t0
{
	pass p0
	{
		VertexShader = compile vs_2_0 VS_HexBatch();
		PixelShader = compile ps_2_0 PS_HexBatch();
		
		AlphaBlendEnable = true;
		SrcBlend = srcAlpha;
		DestBlend = invSrcAlpha;
		CullMode = None;
	}
}

